package com.io.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.io.exception.ScException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class BaseRes {
    /**
     * 返回的状态码，通常来说，会和Http的状态码保持一致：200为成功
     * 400到499为不可重试的错误(例如权限，参数校验失败)
     * 500到599为可重复的错误(网络波动，服务无法连接等)
     */
    private Integer resCode;

    /**
     * 返回的信息，通常只有resCode不是200的时候，才会有用。
     */
    private String resMsg;

    public static BaseRes ok() {
        BaseRes baseRes = new BaseRes();
        baseRes.setResCode(200);
        baseRes.setResMsg("操作成功！");
        return baseRes;
    }

    @JsonIgnore
    public boolean isOk() {
        return resCode != null && resCode == 200;
    }

    public static BaseRes ok(String message) {
        BaseRes baseRes = new BaseRes();
        baseRes.setResCode(200);
        baseRes.setResMsg(message);
        return baseRes;
    }

    public static BaseRes error(Integer code, String info) {
        BaseRes baseRes = new BaseRes();
        baseRes.setResCode(code);
        baseRes.setResMsg(info);
        return baseRes;
    }

    public static BaseRes error(ScException e) {
        BaseRes baseRes = new BaseRes();
        baseRes.setResCode(e.getCode());
        baseRes.setResMsg(e.getMessage());
        return baseRes;
    }
}
